<template>
  <view class="article">
    <scroll-view
      class="article-list"
      scroll-y
      :scroll-anchoring="false"
      refresher-enabled
      :refresher-triggered="triggered"
      @refresherrefresh="onRefresh"
      @refresherrestore="onRestore"
      @scrolltolower="loadMore"
    >
      <template v-for="(item, index) in dataList">
        <ArticleItem
          v-if="item.type == 'article'"
          class="list-item"
          :key="index"
          :item="item.elementData"
          @update="handleUpdate($event, index)"
          @delete="handleDelete($event, index, 'article')"
          @showLoginDialog = "showLoginDialog"
        ></ArticleItem>
        <SpecialIssueItem
          v-if="item.type == 'specialIssue'"
          class="list-item"
          :key="index"
          :item="item.elementData"
          @delete="handleDelete($event, index, 'specialIssue')"
        >
        </SpecialIssueItem>
      </template>
      <u-loadmore
        v-if="dataList.length || !loaded"
        class="list-more"
        :status="moreLoading"
        :load-text="loadText"
      />
      <Empty
        v-if="loaded && !dataList.length"
        :baseImg="emptyImg"
        text="暂无相关职讯"
        :fontWeight="400"
      />
    </scroll-view>
    <LoginDialog ref="loginDialog" @loginSuccessBack="loginSuccessBack"></LoginDialog>
  </view>
</template>

<script>
import ArticleItem from "@/components/ArticleItem";
import SpecialIssueItem from "@/components/SpecialIssueItem";
import { getChannelHomePageData } from "@/api/modules/career";
import LoginDialog from "@/components/LoginDialog";

export default {
  components: {
    ArticleItem,
    SpecialIssueItem,
    LoginDialog,
  },
  data() {
    return {
      personId: uni.getStorageSync("person-id").slice(2) || "",
      dataList: [],
      loaded: false,
      triggered: false,
      _triggered: false,
      emptyImg: this.$imgBaseSelf + "common/empty.png",
      moreLoading: "loadmore",
      loadText: {
        loadmore: "加载更多",
        loading: "加载中",
        nomore: "已经到底啦",
      },
      selectedItem: {},
      showDeleteModal: false,
      deleteData: "",
      deleteIndex: "",
      deleteType: "",
      callback:''
    };
  },

  mounted() {
    this.getData();
  },
  methods: {
    getData(isMore) {
      if (this.moreLoading == "loading" || this.moreLoading == "nomore") return;
      this.moreLoading = "loading";
      const params = {};
      if (this.personId) {
        params.personId = this.personId;
      }
      params.homeVersion = 2;
      getChannelHomePageData(params)
        .then((res) => {
          if (res.code == 200) {
            if (isMore) {
              this.dataList.push(...res.data);
            } else {
              this.dataList = res.data;
            }
            this.moreLoading = "loadingmore";
          }
        })
        .finally(() => {
          this.triggered = false;
          this._triggered = false;
        });
    },
    loadMore() {
      this.getData(true);
    },
    onRestore() {
      this.triggered = false;
      this._triggered = false;
    },
    onRefresh() {
      if (this._triggered) return;
      this._triggered = true;

      if (!this.triggered) {
        this.triggered = true;
      }
      this.getData();
    },
    handleUpdate(data, index) {
      if (data.operateType == "favorite") {
        this.dataList[index].elementData.isFavorite =
          data.type == "add" ? "1" : "0";
      } else if (data.operateType == "praise") {
        this.dataList[index].elementData.isPraise =
          data.type == "add" ? "1" : "0";
        this.dataList[index].elementData.likeCnt =
          data.type == "add"
            ? this.dataList[index].elementData.likeCnt + 1
            : this.dataList[index].elementData.likeCnt - 1;
      }
    },
    handleDelete(data, index, type) {
      this.deleteType = type;
      this.deleteIndex = index;
      this.deleteData = data;
      this.$emit("delete", {
        item: data.item,
        index,
        detail: data.detail,
        type,
      });
    },
    updateLink(linkType, moduleId) {
      if (this.deleteType == "video") {
        this.dataList[this.deleteIndex].elementData.splice(
          this.deleteData.index,
          1
        );
      } else if (this.deleteType == "article") {
        // if (linkType == 1) {
        //   this.dataList = this.dataList.filter((o) => {
        //     console.log(o,o.elementData.personDetail.personId);
        //     return o.elementData.personDetail.personId != moduleId;
        //   })
        // } else {
        //   this.dataList.splice(this.deleteIndex, 1);
        // }
        this.dataList.splice(this.deleteIndex, 1);
      } else if (this.deleteType == "specialIssue") {
        // if (linkType == 1) {
        //   this.dataList = this.dataList.filter((o) => {
        //     return o.elementData.ownId != moduleId;
        //   })
        // } else {
        //   this.dataList.splice(this.deleteIndex, 1);
        // }
        this.dataList.splice(this.deleteIndex, 1);
      }
    },
    showLoginDialog(param,callback){
      console.log("showLoginDialog");
      this.$refs.loginDialog.setVisibility(true);
      this.callback = callback;
    },
    loginSuccessBack(){
      console.log("loginSuccessBack");
      this.personId = uni.getStorageSync("person-id").slice(2) || undefined;
      this.callback("success");
    }
  },
};
</script>

<style lang="scss" scoped>
.article {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #fff;

  &-list {
    box-sizing: border-box;
    height: 1px;
    flex-grow: 1;
  }
}
</style>
